Make dl-snapshot.py avoid dl'ing existing files
authorHans Jørgen Hoel <hansjorg@urkilden.no>
Mon, 18 May 2015 23:27:29 +0000 (01:27 +0200)
committerHans Jørgen Hoel <hansjorg@urkilden.no>
Mon, 18 May 2015 23:27:29 +0000 (01:27 +0200)
Check for existence of file in dl_path before fetching with curl.
If file exists, compare hash with expected.

Also wrap tarfile.open() in contextlib.closing() to support older
python versions (<= 2.6).

This fixes part of #1525.

src/etc/dl-snapshot.py

index e5dfba77246fb5fe8dafd79857e37066be014d9d..c26297471663ab01a193f6cb0538d17cb549de59 100644 (file)
@@ -5,6 +5,7 @@ import subprocess
 import sys
 import tarfile
 import shutil
+import contextlib
 
 with open('src/snapshots.txt') as f:
     lines = f.readlines()
@@ -70,14 +71,22 @@ if not os.path.isdir('target/dl'):
 if os.path.isdir(dst):
     shutil.rmtree(dst)
 
-ret = subprocess.call(["curl", "-o", dl_path, url])
-if ret != 0:
-    raise Exception("failed to fetch url")
-h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest()
-if h != hash:
-    raise Exception("failed to verify the checksum of the snapshot")
+exists = False
+if os.path.exists(dl_path):
+    h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest()
+    if h == hash:
+        print("file already present %s (%s)" % (dl_path, hash,))
+        exists = True
 
-with tarfile.open(dl_path) as tar:
+if not exists:
+    ret = subprocess.call(["curl", "-o", dl_path, url])
+    if ret != 0:
+        raise Exception("failed to fetch url")
+    h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest()
+    if h != hash:
+        raise Exception("failed to verify the checksum of the snapshot")
+
+with contextlib.closing(tarfile.open(dl_path)) as tar:
     for p in tar.getnames():
         name = p.replace("cargo-nightly-" + triple + "/", "", 1)
         fp = os.path.join(dst, name)